﻿using System;
using System.Linq;
using System.DirectoryServices.Protocols;

namespace VPLink2013.Batch
{
    public class Employee
    {
        private string _value = string.Empty;
        private string _fullName = string.Empty;
        private string _lastName = string.Empty;
        private string _firstName = string.Empty;
        private string _login = string.Empty;
        private string _phoneNumber = string.Empty;
        private bool _isManager = false;
        private string _title = string.Empty;
        private string _team = string.Empty;
        private string _teamCode = string.Empty;
        private string _managerValue = string.Empty;
        private string _pictureUrl = string.Empty;
        private string _sitePerso = string.Empty;
        #region Properties

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public string FullName
        {
            get { return _fullName; }
            set { _fullName = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public string Login
        {
            get { return _login; }
            set { _login = value; }
        }

        public string ManagerValue
        {
            get { return _managerValue; }
            set { _managerValue = value; }
        }

        public string PhoneNumber
        {
            get { return _phoneNumber; }
            set { _phoneNumber = value; }
        }

        public bool IsManager
        {
            get { return _isManager; }
            set { _isManager = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Team
        {
            get { return _team; }
            set { _team = value; }
        }

        public string TeamCode
        {
            get { return _teamCode; }
            set { _teamCode = value; }
        }

        public string PictureUrl
        {
            get { return _pictureUrl; }
            set { _pictureUrl = value; }
        }
        public string SitePersonnel
        {
            get { return _sitePerso; }
            set { _sitePerso = value; }
        }
        #endregion

        public Employee(SearchResultEntry resEntry)
        {
            _value = resEntry.AttributeValueAsString(Constants.CommonNameDAPField);
            _lastName = resEntry.AttributeValueAsString(Constants.LastNameLDAPField);
            _firstName = resEntry.AttributeValueAsString(Constants.FirstNameLDAPField);
            _fullName = resEntry.AttributeValueAsString(Constants.DisaplayNameLDAPField);
            _managerValue = resEntry.AttributeValueAsString(Constants.ManagerCodeLDAPField);
            _team = resEntry.AttributeValueAsString(Constants.OUDispalyNameLDAPField);
            _phoneNumber = resEntry.AttributeValueAsString(Constants.TelephoneNumberDAPField);
            _title = resEntry.AttributeValueAsString(Constants.TitleLDAPField).Replace(Constants.LDAP_Init_Prefix, string.Empty);
            _teamCode = resEntry.AttributeValueAsString(Constants.OUCodeLDAPField);

            // Build login
            string mail = resEntry.AttributeValueAsString(Constants.eMailLDAPField);
            _login = resEntry.AttributeValueAsString(Constants.EmptyIfNotManagerLDAPField);

            if (string.IsNullOrEmpty(_login) && !string.IsNullOrEmpty(mail))
            {
                _login = mail.IndexOf(Constants.LDAP_Mail_Domain) > 0
                    ? mail.Replace(string.Format("@{0}", Constants.LDAP_Mail_Domain), string.Empty)
                    : mail.IndexOf(Constants.VP_Mail_Domain) > 0
                    ? mail.Replace(string.Format("@{0}", Constants.VP_Mail_Domain), string.Empty)
                    : string.Empty;
            }
            if (string.IsNullOrEmpty(_login))
            {
                _login = string.Concat(_firstName.Substring(0, 1), _lastName).ToLower();
            }
        }
    }

    public static class SearchResultEntryExtension
    {
        public static string AttributeValueAsString(this SearchResultEntry resEntry, string attribueName)
        {
            string retValue = string.Empty;
            try
            {
                if (resEntry.Attributes[attribueName] != null)
                {
                    if (resEntry.Attributes[attribueName].GetValues(typeof(string)) != null)
                    {
                        if (resEntry.Attributes[attribueName].GetValues(typeof(string)).Count() > 0)
                        {
                            retValue = (string)resEntry.Attributes[attribueName].GetValues(typeof(string))[0];
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //TraceLog.Add(string.Format("Exception Lors de la recherche dans SearchResultEntry la valeur pour l'attribut : {0}", attribueName));
                //TraceLog.Add(ex.Message);
            }
            return retValue;
        }
    }
}
